package dao.Impl;

import com.alibaba.druid.util.JdbcUtils;
import commons.druidJdbcUtil;
import commons.jdbcUtils;
import dao.UserLoginDao;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserLoginDaoImpl implements UserLoginDao {

    @Override
    public User SelectByUsernameAndUserpwd(String username,String userpwd) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        User user=null;

        try {
            conn = druidJdbcUtil.getConnection();
            String sql="select * from Users where username=? and userpwd=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,userpwd);
            rs=ps.executeQuery();
            while(rs.next()){
                user=new User();
                int userid = rs.getInt("userid");
                String username1 = rs.getString("username");
                String usersex = rs.getString("usersex");
                String userpwd1 = rs.getString("userpwd");
                String phoneNum = rs.getString("phoneNum");
                String qqNum = rs.getString("qqNum");

                user.setPhoneNum(phoneNum);
                user.setUsername(username1);
                user.setUserpwd(userpwd1);
                user.setUsersex(usersex);
                user.setQqNum(qqNum);
                user.setUserid(userid);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            druidJdbcUtil.closeResource(conn,ps,rs);
        }
        return user;
    }

}
